/**************************************************************************
 *  创建者：        hualei
 *  开始时间：   2018.06.23
 *  copyright:  (C) 深圳市华友高科有限公司
 *  修改说明：  (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 *
 ***************************************************************************/
#include "tcpstartinstructionview.h"
#include "ui_tcpstartinstructionview.h"
#include "../../programView/programview.h"

TCPStartInstructionView::TCPStartInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TCPStartInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);
    
    tcpStartClientIpVec.resize(4);
    tcpStartClientIpVec[0]=192;
    tcpStartClientIpVec[1]=168;
    tcpStartClientIpVec[2]=1;
    tcpStartClientIpVec[3]=109;
    
    tcpStartServerIpVec.resize(4);
    tcpStartServerIpVec[0] = 192;
    tcpStartServerIpVec[1] = 168;
    tcpStartServerIpVec[2] = 1;
    tcpStartServerIpVec[3] = 169;
    
    ip1 = 0;
    ip2 = 0;
    ip3 = 0;
    ip4 = 0;
}

void TCPStartInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->note_GBox_2->hide();
        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void TCPStartInstructionView::getInput()
{
    if(tcpStartType == E_TCP_START_CLIENT)
    {
        tcpStartServerIpVec[0] = ui->lineEdit_ip1->text().toInt();
        tcpStartServerIpVec[1] = ui->lineEdit_ip2->text().toInt();
        tcpStartServerIpVec[2] = ui->lineEdit_ip3->text().toInt();
        tcpStartServerIpVec[3] = ui->lineEdit_ip4->text().toInt();
    }
    else if(tcpStartType == E_TCP_START_SERVER)
    {
        tcpStartClientIpVec[0]= ui->lineEdit_ip1->text().toInt();
        tcpStartClientIpVec[1]= ui->lineEdit_ip2->text().toInt();
        tcpStartClientIpVec[2]= ui->lineEdit_ip3->text().toInt();
        tcpStartClientIpVec[3]= ui->lineEdit_ip4->text().toInt();
    }
    
    tcpStartPort = ui->PORT_Edit->text().toInt();
    
    jmpLabelValue = ui->JMP_LB_SpinBox->text().toInt();
    
    noteStr = ui->note_lineEdit->text();
    
    
}

void TCPStartInstructionView::updateView()
{
    QString ip;
    ui->CLIENT_radioButton->setChecked(false);
    ui->SERVER_radioButton->setChecked(false);

    if(tcpStartType == E_TCP_START_CLIENT)
    {
        ui->IP_Label->setText(tr("Server IP :"));
        ui->CLIENT_radioButton->setChecked(true);
        ip = QString::number(tcpStartServerIpVec[0]);
        ui->lineEdit_ip1->setText(ip);
        ip = QString::number(tcpStartServerIpVec[1]);
        ui->lineEdit_ip2->setText(ip);
        ip = QString::number(tcpStartServerIpVec[2]);
        ui->lineEdit_ip3->setText(ip);
        ip = QString::number(tcpStartServerIpVec[3]);
        ui->lineEdit_ip4->setText(ip);

    }
    else if(tcpStartType == E_TCP_START_SERVER)
    {
        ui->IP_Label->setText(tr("Client IP :"));
        ui->SERVER_radioButton->setChecked(true);
        ip = QString::number(tcpStartClientIpVec[0]);
        ui->lineEdit_ip1->setText(ip);
        ip = QString::number(tcpStartClientIpVec[1]);
        ui->lineEdit_ip2->setText(ip);
        ip = QString::number(tcpStartClientIpVec[2]);
        ui->lineEdit_ip3->setText(ip);
        ip = QString::number(tcpStartClientIpVec[3]);
        ui->lineEdit_ip4->setText(ip);      
    }
    
    ui->PORT_Edit->setText(QString::number(tcpStartPort));
    ui->JMP_LB_SpinBox->setValue(jmpLabelValue);
    ui->note_lineEdit->setText(noteStr);
    
    
}

void TCPStartInstructionView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/TCPStartInstructionViewEnglish.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);
}

void TCPStartInstructionView::on_CLIENT_radioButton_clicked()
{
    ui->IP_Label->setText(tr("Server IP :"));
    ui->lineEdit_ip1->setText(QString::number(ip1));
    ui->lineEdit_ip2->setText(QString::number(ip2));
    ui->lineEdit_ip3->setText(QString::number(ip3));
    ui->lineEdit_ip4->setText(QString::number(ip4));
    tcpStartType = E_TCP_START_CLIENT;

}

void TCPStartInstructionView::on_SERVER_radioButton_clicked()
{
    ip1 = ui->lineEdit_ip1->text().toInt();
    ip2 = ui->lineEdit_ip2->text().toInt();
    ip3 = ui->lineEdit_ip3->text().toInt();
    ip4 = ui->lineEdit_ip4->text().toInt();

    ui->IP_Label->setText(tr("Local IP :"));
    ui->lineEdit_ip1->setText("127");
    ui->lineEdit_ip2->setText("0");
    ui->lineEdit_ip3->setText("0");
    ui->lineEdit_ip4->setText("1");

    tcpStartType = E_TCP_START_SERVER;

}

void TCPStartInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit cleanNoteSignal();

}

void TCPStartInstructionView::on_forward_Btn_clicked()
{

    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();

}

void TCPStartInstructionView::on_save_Btn_clicked()
{
    getInput();
    emit finishEditInstructionSignal();
    emit saveInstructionSignal(instructionOut);

}
